//
//  ClubDetailViewController.swift
//  YueJian
//
//  Created by Milo.o on 2017/11/2.
//  Copyright © 2017年 MapleStory. All rights reserved.
//

import UIKit
import Alamofire

class ClubDetailViewController: UITableViewController {

    var id = ""
    var detail = NSDictionary();
    //    let arrayTitle = ["跆拳道（韩文：태권도，英文：Taekwondo），是现代奥运会正式比赛项目之一，是一种主要使用手及脚进行格斗或对抗的运动。",
    //                      "跆拳道是经过东亚文化发展的一项韩国武术，以“始于礼，终于礼”的武道精神为基础。其脚法占70%。跆拳道的套路共有25套；另外还有兵器、擒拿、摔锁、对拆自卫术及10余种基本功夫等",
    //                      "2016年5月8号","大连市"]
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Alamofire.request(URL(string: "http://172.24.10.175/workout/api.php/get/mod/club")!, method: .get, parameters: ["id":id], encoding: URLEncoding.default, headers: nil).responseJSON { (responcse) in
            self.detail = responcse.result.value as! NSDictionary
            print(responcse.result.value)
            self.tableView?.reloadData()
            
            self.title = self.detail["club_name"] as? String
        }
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
    
//    override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
//
//
//    }
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return ""
        } else if section == 1 {
           return ""
        }
        else if section == 2 {
//            let arrya = ["活动简介", "立即报名"]
            return "活动简介"
        } else {
            return "立即报名"
        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        cell.textLabel?.numberOfLines = 0
        if detail.count != 0 {
            switch indexPath.section {
            case 0:
                let str = detail["thumb"] as? String
                print(str)
                let imageStr = "http://172.24.10.175/workout/Uploads/"+(str ?? "")
                let images = try! UIImage(data: Data(contentsOf: URL(string: imageStr)!))
                cell.backgroundView = UIImageView(image: images)
            case 1:
                cell.textLabel?.text = "地址:  "  + (self.detail["city_name"] as! String)
                cell.backgroundView = nil
                cell.detailTextLabel?.text = "报名电话:  " + (self.detail["club_tel"] as! String)
            case 2:
                cell.backgroundView = nil
                cell.textLabel?.text = self.detail["introduction"] as? String
            default:
                let button = UIButton(frame: CGRect(x: 50, y: 20, width: (Screen_Width)/5, height: 50))
                
                button.setTitle("立即报名", for: .normal)
                button.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
                
                button.titleLabel?.textColor = UIColor.black
                 button.addTarget(self, action: #selector(baoming), for: UIControlEvents.touchUpInside)
                cell.addSubview(button)
                
            }
        }
        
        return cell
    }
    

    @objc func baoming(btn:UIButton) {
        let alertVC = UIAlertController(title: "提示", message: "报名成功", preferredStyle: UIAlertControllerStyle.alert)
        let acSure = UIAlertAction(title: "确定", style: UIAlertActionStyle.destructive) { (UIAlertAction) -> Void in
            print("click Sure")
        }
        let acCancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) { (UIAlertAction) -> Void in
            print("click Cancel")
        }
        alertVC.addAction(acSure)
        alertVC.addAction(acCancel)
//        self.presentViewController(alertVC, animated: true, completion: nil)
        self.present(alertVC, animated: true) {
            
        }
        
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 150
        } else {
            return 100
        }
    }
    
    
    /*
     // Override to support conditional editing of the table view.
     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the specified item to be editable.
     return true
     }
     */
    
    /*
     // Override to support editing the table view.
     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
     if editingStyle == .delete {
     // Delete the row from the data source
     tableView.deleteRows(at: [indexPath], with: .fade)
     } else if editingStyle == .insert {
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
     }
     }
     */
    
    /*
     // Override to support rearranging the table view.
     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
     
     }
     */
    
    /*
     // Override to support conditional rearranging of the table view.
     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the item to be re-orderable.
     return true
     }
     */
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */

}
